package logic

import (
	"context"

	"github.com/comfygpt-backend-go/common/model"
	"github.com/comfygpt-backend-go/service/user-rpc/internal/svc"
	"github.com/comfygpt-backend-go/service/user-rpc/user"

	"github.com/zeromicro/go-zero/core/logc"
	"github.com/zeromicro/go-zero/core/logx"
)

type UploadLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUploadLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UploadLogic {
	return &UploadLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UploadLogic) Upload(in *user.UploadRequest) (*user.UpdateInfoResponse, error) {
	// 校验参数
	// 调用第三方服务
	url, err := l.svcCtx.MinioCli.PutObject(l.ctx, l.svcCtx.Config.MinioAuth.Bucket, in.ObjectName, in.Image)
	if err != nil {
		return nil, err
	}
	logc.Info(l.ctx, "上传成功: ", url)
	// 更新本地数据
	userInfo := &model.User{
		Id:                in.UserId,
		Profile_image_url: url,
	}

	err = l.svcCtx.UserModel.Update(l.ctx, userInfo)
	if err != nil {
		return nil, err
	}

	return &user.UpdateInfoResponse{}, nil
}
